import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:wan_android_flutter/repository/datas/knowledge_list_model.dart';
import 'package:wan_android_flutter/routes/RouteUtils.dart';

import '../../routes/routes.dart';
import 'knowledge_vm.dart';

class KnowledgePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _KnowledgePageState();
  }
}

class _KnowledgePageState extends State<KnowledgePage> {

  KnowledgeViewModel knowledgeViewModel = KnowledgeViewModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    knowledgeViewModel.getKnowledgeList();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ChangeNotifierProvider(create: (create){
      return knowledgeViewModel;
    },child: Consumer<KnowledgeViewModel>(builder: (context,vm,child){
      return ListView.builder(
        itemCount: vm.knowledgeList?.length ?? 0,
        itemBuilder: (context, index) {
          return _itemView(vm.knowledgeList?[index],onTap: (){
            RouteUtils.pushForNamed(context, RoutesPath.knowledgeDetailTabPage,arguments:{"tabs":vm.knowledgeList?[index]?.children});
          });
        },
      );
    }));
  }
  Widget _itemView(KnowledgeModel? item,{GestureTapCallback? onTap}){
    return Container(
      margin: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black12, width: 1),
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
      ),
      child: ListTile(
        title: Text(item?.name ?? "", style: TextStyle(fontWeight: FontWeight.bold)),
        subtitle: Text(knowledgeViewModel.getGeneralSubTitle(item?.children ?? [])),
        trailing: SizedBox(
          width: 20,
          height: 20,
          child: Image.asset("assets/images/img_arrow_right.png",width: 20.w,height: 20.h,),
        ),
        onTap: onTap,
      ),
    );
  }
}
